//
//  ActiveViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/5/10.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class ActiveViewController: BaseViewController, UITableViewDelegate,UITableViewDataSource {
    
    var headerView: UIView!
    var tableView: UITableView!
    
    var array = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.setNewTitle("活跃度排行")
        
        self.array = getLocationData(key: activeArray)
        
        InitViews()
    }
    
    
    func InitViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.backgroundColor = UIColor.clear
        tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        
        setUpRefresh()
    }
    
    func setUpRefresh() {
        tableView.header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(ActiveViewController.refresh))
        tableView.header.beginRefreshing()
    }
    
    func refresh() {
        let user = getUserPojo()
        let requestXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><t>6</t><SuperType>\(user.SupperType)</SuperType><UpperCode>\(user.UpperCode!)</UpperCode></channel></rss>"
        let url = server + superWebapp
        HttpPostUTF(url, requestXML: requestXml) { (data, result, error) in
            if error == nil {
                //++++++++++++++++++++++++++++++++++++++++++++++++
//                self.array = JsonFormt(str: result)
                //++++++++++++++++++++++++++++++++++++++++++++++++
                
                setLocationData(result!, key: activeArray)
                self.array = getLocationData(key: activeArray)
                
                //++++++++++++++++
                self.tableView.reloadData()
                self.tableView.header.endRefreshing()
            }else{
                let alert: UIAlertView = UIAlertView.init(title: "错误提示", message: error!.localizedDescription, delegate: nil, cancelButtonTitle: "知道了")
                alert.show()
                self.tableView.header.endRefreshing()
            }
        }
        
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "SchCountTableViewCellIdentifier";
        
        let cell: SchCountTableViewCell = SchCountTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        let dic = array[indexPath.row] as! JSON
        let schName = dic["schname"].stringValue
        let count = dic["strper"].stringValue
        cell.schName.text = schName
        cell.count.text = "活跃度：\(count)%"
        
        
        return cell
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
